import { defineStore } from 'pinia'
import { ref } from 'vue'
import { newsGetTask } from '~/api/newsApi' // 假设你的API文件在src/api目录下

interface NewsItem {
    id: string
    title: string
    publish_time: string
    paragraph1: string
    paragraph2: string
    image1: string
    summary: string
}

export const useNewsStore = defineStore('news', () => {
    const newsList = ref<NewsItem[]>([])
    const loading = ref(false)
    const error = ref<string | null>(null)

    const fetchNews = async () => {
        try {
            loading.value = true
            error.value = null
            const response = await newsGetTask()

            if (response.success && response.data) {
                newsList.value = response.data
            } else {
                error.value = response.msg || '获取新闻失败'
            }
        } catch (err) {
            error.value = '网络错误，请稍后重试'
        } finally {
            loading.value = false
        }
    }

    return { newsList, loading, error, fetchNews }
})